package cn.edu.scau.dormitory_management_system.service.impl;

import cn.edu.scau.dormitory_management_system.dao.DormDao;
import cn.edu.scau.dormitory_management_system.dao.SysLogDao;
import cn.edu.scau.dormitory_management_system.entity.*;
import cn.edu.scau.dormitory_management_system.entity.dto.StuDTO;
import cn.edu.scau.dormitory_management_system.service.BedService;
import cn.edu.scau.dormitory_management_system.service.DormService;
import cn.edu.scau.dormitory_management_system.service.DormbuildService;
import cn.edu.scau.dormitory_management_system.service.StudentService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


@Service("dormService")
public class DormServiceImpl extends ServiceImpl<DormDao, DormEntity> implements DormService {

    @Autowired
    private BedService bedService;

    @Autowired
    private DormbuildService dormbuildService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private DormDao dormDao;

    @Override
    public boolean create( DormEntity dormEntity){
        return this.baseMapper.insert(dormEntity) > 0;
    }

    @Override
    public boolean delete( DormEntity dormEntity){
        boolean flag = this.baseMapper.deleteById(dormEntity) > 0;
        if(!flag) return flag;
        flag = bedService.deleteBed(dormEntity.getDormId()) > 0;
        return flag;
    }

    @Override
    public int max() {
        return this.baseMapper.max();
    }

    @Override
    public boolean update( DormEntity dormEntity){
        return this.baseMapper.updateById(dormEntity) > 0;
    }

    @Override
    public boolean deleteDormInBuild( int buildId){
        List<DormEntity> dormEntities = this.baseMapper.selectList(new QueryWrapper<DormEntity>().eq("build_id",buildId));
        for (DormEntity dormEntity:dormEntities
             ) {
            bedService.deleteBed(dormEntity.getDormId());
        }
        return this.baseMapper.delete(new QueryWrapper<DormEntity>().eq("build_id",buildId)) > 0;
    }

    @Override
    public List<DormEntity> findDormByBuildId( int buildId){
        List<DormEntity> dormEntities = this.baseMapper.selectList(new QueryWrapper<DormEntity>().eq("build_id", buildId));
        return dormEntities;
    }

    @Override
    public List<DormEntity> findDormByDepartment( String department){
        List<DormEntity> dormEntities = this.baseMapper.selectList(new QueryWrapper<DormEntity>().eq("department", department));
        return dormEntities;
    }

    @Override
    public List<DormEntity> findDormByClazz(String clazz){
        List<DormEntity> dormEntities = this.baseMapper.selectList(new QueryWrapper<DormEntity>().eq("clazz", clazz));
        return dormEntities;
    }

    //查询某楼栋闲置房间
    @Override
    public List<DormEntity> findSpareDorm( int buildId){
        List<DormEntity> dormEntities = this.baseMapper.selectList(new QueryWrapper<DormEntity>().eq("status", 0).eq("build_id",buildId));
        return dormEntities;
    }

    @Override
    public DormEntity findDormByName( String dormName){
        DormEntity dormEntity = this.baseMapper.selectOne(new QueryWrapper<DormEntity>().eq("dorm_name", dormName));
        return dormEntity;
    }

    @Override
    public List<StuDTO> getStudentDto(int buildId){
        QueryWrapper<DormEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("build_id", buildId);
        List<DormEntity> dormEntities = this.baseMapper.selectList(wrapper);
        List<StuDTO> ans = new ArrayList<>();
        String dormBuildName = dormbuildService.getById(buildId).getDormBuildName();
        for (DormEntity dormEntity : dormEntities) {
            if(dormEntity == null) continue;
            if(dormEntity.getDormId() == null) continue;
            QueryWrapper<BedEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("dorm_id", dormEntity.getDormId());
            List<BedEntity> bedEntities = bedService.getBaseMapper().selectList(queryWrapper);
            for (BedEntity bedEntity : bedEntities) {
                if(bedEntity == null) continue;
                if(bedEntity.getStuId() == null) continue;
                StuDTO stuDTO = new StuDTO();
                stuDTO.setBuildId(dormEntity.getBuildId());
                stuDTO.setBuildName(dormBuildName);
                stuDTO.setDormName(dormEntity.getDormName());
                stuDTO.setId(bedEntity.getStuId());
                QueryWrapper<StudentEntity> wrapper1 = new QueryWrapper<>();
                wrapper1.eq("stu_id", bedEntity.getStuId());
                StudentEntity one = studentService.getOne(wrapper1);
                stuDTO.setName(one.getStuName());
                stuDTO.setAcademic(one.getAcademic());
                stuDTO.setGrade(one.getGrade());
                stuDTO.setClazz(one.getClazz());
                stuDTO.setDepartment(one.getDepartment());
                ans.add(stuDTO);
            }
        }
        return ans;
    }

    @Override
    public IPage<StuDTO> getStuDTO(IPage<StuDTO> page, int dormId) {
        return dormDao.getStuDTO(page,dormId);
    }

    @Override
    public DormEntity select(String academic, String clazz, String department) {
        return dormDao.getDormOne(academic, clazz, department);
    }
}